﻿using Abp;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using FwProject.Shop.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.BrandInfo;
using FwProject.ShoppingMall.Shop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;

namespace FwProject.Shop
{
    public class ShopProductTagAppService : FwProjectAppServiceBase, IShopProductTagAppService
    {
        private readonly IRepository<ShopProductTag, Guid> _repositoryShopProductTag;
        private readonly IRepository<ShopProduct, Guid> _repositoryShopProduct;
        private readonly IRepository<Brand, Guid> _repositoryBrand;
        private readonly IRepository<ShopType, Guid> _repositoryShopType;

        public IGuidGenerator _guid { get; set; }
        public ShopProductTagAppService(IRepository<ShopProductTag, Guid> repositoryShopProductTag,
            IRepository<ShopProduct, Guid> repositoryShopProduct,
            IRepository<Brand, Guid> repositoryBrand,
            IRepository<ShopType, Guid> repositoryShopType)
        {
            _repositoryShopProductTag = repositoryShopProductTag;
            _repositoryShopProduct = repositoryShopProduct;
            _repositoryBrand = repositoryBrand;
            _repositoryShopType = repositoryShopType;
            _guid = SequentialGuidGenerator.Instance;
        }
        public async Task<LayerDataBaseDto> GetShopProductTagByTagid(int page, int limit, Guid tagId)
        {
            var query = _repositoryShopProductTag.GetAll().Where(o => o.Tagid == tagId).Join(_repositoryShopProduct.GetAll(), i => i.ShopProductid, j => j.Id,
                (producttag, shop) => new ShopProductByTagIdDto
                {
                    Id = producttag.Id,
                    ProductName = shop.ProductName,
                    ProductNumber = shop.ProductNumber,
                });

            var pagelist = await query.OrderByDescending(o => o.Id).ToPagedListAsync(page, limit);

            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;

            return dto;
        }

        public async Task<LayerDataBaseDto> GetNotTagShopDataByTagid(int page, int limit, Guid tagId)
        {
            var shopProductids = _repositoryShopProductTag.GetAll().Where(o => o.Tagid == tagId).Select(o => o.ShopProductid);

            var query = _repositoryShopProduct.GetAll().Where(o => !shopProductids.Any(s => s == o.Id)).Join(_repositoryShopType.GetAll(), i => i.ShopTypeid, j => j.Id, (shop, type) => new
            {
                shop.Id,
                type.TypeName,
                shop.ProductName,
                shop.ProductNumber,
                shop.SpcemarketPrice,
                shop.IsGrounding,
                shop.Brandid,
                shop.CreationTime,
                TypeId = type.Id
            }).Join(_repositoryBrand.GetAll(), i => i.Brandid, j => j.Id,
                (shop, brand) => new ShopProductOutputDto
                {
                    Id = shop.Id,
                    Category = shop.TypeName,
                    ProductName = shop.ProductName,
                    ProductNumber = shop.ProductNumber,
                    SpcemarketPrice = shop.SpcemarketPrice,
                    IsGrounding = shop.IsGrounding ? "是" : "否",
                    Brands = brand.BrandName,
                    CreationTime = shop.CreationTime,
                    TypeId = shop.TypeId
                });

            var pagelist = await query.OrderByDescending(o => o.Id).ToPagedListAsync(page, limit);

            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;

            return dto;
        }


        public async Task CreateAsync(Guid tagid, string shopids)
        {
            var shops = shopids.Split('|');

            foreach (var item in shops)
            {
                ShopProductTag entity = new ShopProductTag();
                entity.Id = _guid.Create();
                entity.Tagid = tagid;
                entity.ShopProductid = new Guid(item);
                await _repositoryShopProductTag.InsertAndGetIdAsync(entity);
            }
        }

        public async Task DeleteAsync(Guid id)
        {
            await _repositoryShopProductTag.DeleteAsync(id);
        }

    }
}
